package com.zx.viewlearn.views.pullableview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

/**
 * Author     Sdz
 * Email      946616236@qq.com
 * Time       2016/11/11 0011
 * Function   复写可刷新的ScrollView
 */
public class PullableScrollView extends ScrollView implements Pullable {
    private static final String TAG = "PullableScrollView";
    private int downX;
    private int downY;
    private int mTouchSlop;
    private boolean canPullUp = true;//是否可以上拉
    private boolean canPullDown = true;//是否可以下拉
    private ScrollViewListener listener;
    private ScrollViewTopListener topListener;

    public PullableScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public PullableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public PullableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean canPullDown() {
        return (getScrollY() == 0 && canPullDown);
    }

    @Override
    public boolean canPullUp() {
        return (getScrollY() >= (getChildAt(0).getHeight() - getMeasuredHeight()) && canPullUp);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //专为RecyclerView设计的
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) ev.getRawX();
                downY = (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) ev.getRawY();
                if (Math.abs(moveY - downY) > mTouchSlop) {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }

    //设置是否可以下拉
    public void setCanPullDown(boolean canPullDown) {
        this.canPullDown = canPullDown;
    }

    //设置是否可以上拉
    public void setCanPullUp(boolean canPullUp) {
        this.canPullUp = canPullUp;
    }

    //定义滑动接口
    public interface ScrollViewListener {
        void onScrollChanged(PullableScrollView scrollView, int x, int y, int oldx, int oldy);
    }

    public ScrollViewListener getListener() {
        return listener;
    }

    public void setListener(ScrollViewListener listener) {
        this.listener = listener;
    }

    public interface ScrollViewTopListener {
        void onTop(boolean isVisibility);
    }

    public ScrollViewTopListener getTopListener() {
        return topListener;
    }

    public void setTopListener(ScrollViewTopListener topListener) {
        this.topListener = topListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        Log.i(TAG, "onScrollChanged: \noldt:" + oldt + "\nt:" + t);
        if (listener != null) {
            listener.onScrollChanged(this, l, t, oldl, oldt);
        }
        if (topListener != null){
            topListener.onTop(t <= 0 /*&& oldt > 0*/);
        }
    }

    @Override
    public void setOnScrollChangeListener(OnScrollChangeListener l) {
        super.setOnScrollChangeListener(l);
    }
}
